package cn.edu.klmy.dao.impl;

import cn.edu.klmy.dao.AbstractJdbcTemplate;
import cn.edu.klmy.dao.IParamDao;
import cn.edu.klmy.models.param.SystemParam;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author 顾伟
 *         创建日期：13-4-14
 */
@Repository
public class ParamDao extends AbstractJdbcTemplate implements IParamDao {
    public SystemParam getCurrentParam() {
        String PARAM_QUERY = "SELECT ELECTIVE_TERM,ELECTIVE_START,ELECTIVE_END,ELECTIVE_SECTION FROM ELECTIVE_PARAM WHERE ID=1";
        return (SystemParam) this.jdbcTemplate.queryForObject(PARAM_QUERY, new SystemParamRowMapper());
    }

    public boolean updateCurrentParam(SystemParam param) {
        String PARAM_UPDATE = "UPDATE ELECTIVE_PARAM SET ELECTIVE_TERM=?,ELECTIVE_SECTION=?,ELECTIVE_START=?,ELECTIVE_END=? WHERE ID=1";
        int term =param.getTerm();
        int section = param.getSection();
        int count=  this.jdbcTemplate.update(PARAM_UPDATE, new Object[]{term, section,param.getElectiveStart() , param.getElectiveEnd()});
        if(count==1){
            return true;
        }
        return false;
    }

    private class SystemParamRowMapper implements RowMapper {

        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            SystemParam param = new SystemParam();
            param.setTerm(resultSet.getInt("ELECTIVE_TERM"));
            param.setSection(resultSet.getInt("ELECTIVE_SECTION"));
            param.setElectiveStart(resultSet.getString("ELECTIVE_START"));
            param.setElectiveEnd(resultSet.getString("ELECTIVE_END"));
            return param;
        }
    }
}
